package com.nulldev.util.net.packets.generic;

import java.util.ArrayList;

import com.nulldev.util.math.MathUtil;
import com.nulldev.util.net.packets.APILevel;
import com.nulldev.util.net.packets.NetInfo;
import com.nulldev.util.net.packets.NetPacket;
import com.nulldev.util.net.packets.PacketManager;
import com.nulldev.util.net.packets.UniversalPacket;

@APILevel(level = 1)
public class PacketAPILevel extends UniversalPacket {

	public PacketAPILevel() {
		super("Peer API Level", "api.level", "level{0}");
	}

	@Override
	public void client(byte[] data, byte[] raw, NetInfo client) {
	}

	@Override
	public void server(byte[] data, byte[] raw, NetInfo connection) {
		final ArrayList<Integer> apiLevels = new ArrayList<Integer>();
		for (final NetPacket packet : PacketManager.getClone()) {
			apiLevels.add(packet.getAPILevel());
		}
		final int average = MathUtil.averageI(apiLevels);
		connection.sendBlocking(PacketManager.fromID("api.level", "level{" + String.valueOf(average) + "}"));
	}

}
